package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.en.DictCode;
import com.atguigu.en.HouseStatus;
import com.atguigu.entity.*;
import com.atguigu.result.HouseTypeEnum;
import com.atguigu.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: 翟文海
 * @Date: 2022/10/28/028 23:09
 * @Version: 1.0
 * @Description:
 **/
@Controller
@RequestMapping("/house")
public class HouseController extends BaseController {
    private static final String PAGE_INDEX = "house/index";
    private static final String PAGE_CREATE = "house/create";
    private static final String PAGE_SUCCESS = "common/successPage";
    private static final String PAGE_EDIT = "house/edit";
    private static final String LIST_HOUSE = "redirect:/house";
    private static final String PAGE_SHOW = "house/show";

    @Reference
    private HouseService houseService;
    @Reference
    private DictService dictService;

    @Reference
    private CommunityService communityService;

    @Reference
    private HouseImageService houseImageService;

    @Reference
    private HouseBrokerService houseBrokerService;

    @Reference
    private HouseUserService houseUserService;

    @RequestMapping
    public String index(@RequestParam Map<String, String> filters, Model model) {
        PageInfo<House> page = houseService.findPage(filters);
        model.addAttribute("page", page);
        model.addAttribute("filters", filters);
        saveAllDictToRequestScope(model);

        return toPage(model, null, PAGE_INDEX);
    }

    public void saveAllDictToRequestScope(Model model) {
        List<Community> communityList = communityService.findAll();
        model.addAttribute("communityList", communityList);
        //户型
        List<Dict> houseTypeList = dictService.findByDictCode(DictCode.HOUSETYPE.code);
        //楼层
        List<Dict> floorList = dictService.findByDictCode(DictCode.FLOOR.code);
        List<Dict> buildStructureList = dictService.findByDictCode(DictCode.BUILDSTRUCTURE.code);
        List<Dict> decorationList = dictService.findByDictCode(DictCode.DECORATION.code);
        List<Dict> directionList = dictService.findByDictCode(DictCode.DIRECTION.code);
        List<Dict> houseUseList = dictService.findByDictCode(DictCode.HOUSEUSE.code);
        model.addAttribute("houseTypeList", houseTypeList);
        model.addAttribute("floorList", floorList);
        model.addAttribute("buildStructureList", buildStructureList);
        model.addAttribute("directionList", directionList);
        model.addAttribute("decorationList", decorationList);
        model.addAttribute("houseUseList", houseUseList);
    }

    @GetMapping("/create")
    public String create(Model model) {
        saveAllDictToRequestScope(model);
        return toPage(model, null, PAGE_CREATE);

    }

    @PostMapping("/save")
    public String save(House house, Model model) {
        house.setStatus(HouseStatus.UNPUBLISHED.code);
        houseService.insert(house);
        return toPage(model, "新增房源成功", PAGE_SUCCESS);

    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, Model model) {
        House house = houseService.getById(id);
        model.addAttribute("house", house);
        saveAllDictToRequestScope(model);
        return toPage(model, null, PAGE_EDIT);

    }

    @PostMapping("/update")
    public String update(House house, Model model) {
        houseService.update(house);
        return toPage(model, "修改房源成功", PAGE_SUCCESS);
    }

    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id, Model model) {
        houseService.delete(id);
        return toPage(model, "删除房源成功", LIST_HOUSE);
    }

    @GetMapping("/publish/{id}/{status}")
    public String publish(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        House house = new House();
        house.setId(id);
        house.setStatus(status);
        houseService.update(house);
        return toPage(LIST_HOUSE);
    }


    @GetMapping("/{id}")
    public String show(@PathVariable("id") Long id, Model model) {
        //根据id查询当前当前房源
        House house = houseService.getById(id);

        //根据当前house的小区id外键查询hse_community小区的信息
        Community community = communityService.getById(house.getCommunityId());
        //1为房源图片，2为房产图片
        List<HouseImage> houseImage1List = houseImageService.findHouseImageList(id, HouseTypeEnum.HOUSE_TYPE_RESOURCE.getType());
        //查询房产图片
        List<HouseImage> houseImage2List = houseImageService.findHouseImageList(id, HouseTypeEnum.HOUSE_TYPE_PROPERTY.getType());
        //经纪人信息
        List<HouseBroker> houseBrokerList = houseBrokerService.findHouseBrokerListByHouseId(id);
        //房东信息
        List<HouseUser> houseUserList = houseUserService.findHouseUserListByHouseId(id);
        model.addAttribute("house", house);
        model.addAttribute("community",community);
        model.addAttribute("houseImage1List",houseImage1List);
        model.addAttribute("houseImage2List",houseImage2List);
        model.addAttribute("houseBrokerList",houseBrokerList);
        model.addAttribute("houseUserList",houseUserList);
        return toPage(PAGE_SHOW);
    }
}
